﻿using System;
using System.Collections.Generic;

public class MethodsForIEnumerable
{
    //Implement a set of extension methods for IEnumerable<T> 
    //that implement the following group functions: sum, product, min, max, average.
    
    //SUM
    public static decimal Sum<T>(this IEnumerable<T> myEnumerable)
    {
        decimal counter = 0;
        foreach (var item in myEnumerable)
        {
            counter += Convert.ToDecimal(item);
        }
        return counter;
    }

    //Product
    public static decimal Product<T>(this IEnumerable<T> myEnumerable) 
    {
        decimal counter = 1;
        foreach (var item in myEnumerable)
        {
            counter *= Convert.ToDecimal(item);
        }
        return counter;
    }

    //Min
    public static decimal Min<T>(IEnumerable<T> myEnumerable) 
    {
        decimal minimum=decimal.MaxValue;
        foreach (var item in myEnumerable)
        {
            if (Convert.ToDecimal(item)<minimum)
            {
                minimum = Convert.ToDecimal(item);
            }
        }
        return minimum;
    }


    //Man
    public static decimal Max<T>(IEnumerable<T> myEnumerable)
    {
        decimal maximum = decimal.MinValue;
        foreach (var item in myEnumerable)
        {
            if (Convert.ToDecimal(item) < maximum)
            {
                maximum = Convert.ToDecimal(item);
            }
        }
        return maximum;
    }

    //average
    public static decimal Average<T>(IEnumerable<T> myEnumerable) 
    {
        decimal counter = 0;
        decimal sum = 0;
        foreach (var item in myEnumerable)
        {
            counter++;
            sum = sum + Convert.ToDecimal(item);
        }
        decimal avrg = sum/counter;
        return avrg;
    }

    static void Main()
    {
        List<decimal> myList = new List<decimal>();
        for (int i = 1; i <= 5; i++)
        {
            myList.Add(i);
        }

        Console.WriteLine(Average(myList));
        Console.WriteLine();
        Console.WriteLine();
    }
}